//
//  chartVC.m
//  CMAccountBook
//
//  Created by 王开政 on 2018/10/30.
//  Copyright © 2018年 王开政. All rights reserved.
//

#import "chartVC.h"
#import "LRSChartView.h"
#import "UIColor+Expanded.h"
#import "chartTableViewCell.h"

#import "WLPieView.h"
#define PNLightGreen    [UIColor colorWithRed:77.0 / 255.0 green:216.0 / 255.0 blue:122.0 / 255.0 alpha:1.0f]
#define PNFreshGreen    [UIColor colorWithRed:77.0 / 255.0 green:196.0 / 255.0 blue:122.0 / 255.0 alpha:1.0f]
#define PNDeepGreen     [UIColor colorWithRed:77.0 / 255.0 green:176.0 / 255.0 blue:122.0 / 255.0 alpha:1.0f]
#define PNRed           [UIColor colorWithRed:245.0 / 255.0 green:94.0 / 255.0 blue:78.0 / 255.0 alpha:1.0f]
#define PNMauve         [UIColor colorWithRed:88.0 / 255.0 green:75.0 / 255.0 blue:103.0 / 255.0 alpha:1.0f]
#define PNBrown         [UIColor colorWithRed:119.0 / 255.0 green:107.0 / 255.0 blue:95.0 / 255.0 alpha:1.0f]
#define PNBlue          [UIColor colorWithRed:82.0 / 255.0 green:116.0 / 255.0 blue:188.0 / 255.0 alpha:1.0f]
#define PNDarkBlue      [UIColor colorWithRed:121.0 / 255.0 green:134.0 / 255.0 blue:142.0 / 255.0 alpha:1.0f]
#define PNYellow        [UIColor colorWithRed:242.0 / 255.0 green:197.0 / 255.0 blue:117.0 / 255.0 alpha:1.0f]
#define PNWhite         [UIColor colorWithRed:255.0 / 255.0 green:255.0 / 255.0 blue:255.0 / 255.0 alpha:1.0f]
#define PNDeepGrey      [UIColor colorWithRed:99.0 / 255.0 green:99.0 / 255.0 blue:99.0 / 255.0 alpha:1.0f]
#define PNPinkGrey      [UIColor colorWithRed:200.0 / 255.0 green:193.0 / 255.0 blue:193.0 / 255.0 alpha:1.0f]
#define PNHealYellow    [UIColor colorWithRed:245.0 / 255.0 green:242.0 / 255.0 blue:238.0 / 255.0 alpha:1.0f]
#define PNLightGrey     [UIColor colorWithRed:225.0 / 255.0 green:225.0 / 255.0 blue:225.0 / 255.0 alpha:1.0f]

@interface chartVC ()<UITableViewDelegate,UITableViewDataSource>{
    
    WLPieView *pieChartView;
}
@property(nonatomic,strong)UITableView *Tab;

@property(nonatomic, strong) UIView *headView;

@property(nonatomic,strong)UIView *navigationView;

@property(nonatomic,strong)UILabel *titleLabel;

@property(nonatomic,strong)UIButton *rightButton;

@property(nonatomic,strong)UIButton *yearButton;

@property(nonatomic,strong)UIButton *monthButton;

@property(nonatomic,strong)UIButton *dateButton;


@property (nonatomic, strong) LRSChartView *incomeChartLineView;
@end

@implementation chartVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.navigationBarHidden=YES;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    [self.view addSubview:self.navigationView];
    
    [self createIncomeChartLineView];
    [self addpie];
    [self.view addSubview:self.Tab];
    
    // Do any additional setup after loading the view.
}
-(UIView *)navigationView{
    if (!_navigationView) {
        _navigationView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, SafeAreaTopHeight)];
        _navigationView.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
        [_navigationView addSubview:self.titleLabel];
        [_navigationView addSubview:self.rightButton];
    }
    return _navigationView;
}
-(UILabel *)titleLabel{
    
    if (!_titleLabel) {
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight - 44, ScreenWidth, 44)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = KblackColor;
        [_titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17*kfit]];
        _titleLabel.text = @"收入";
        
    }
    return _titleLabel;
}
-(UIButton *)rightButton{
    if (!_rightButton) {
        _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-60*kfit, SafeAreaTopHeight-40*kfit, 40*kfit, 40*kfit)];
        [_rightButton setImage:[UIImage imageNamed:@"签到-1"] forState:UIControlStateNormal];
        
        _rightButton.tag = 1000;
        [_rightButton addTarget:self action:@selector(rightPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}
-(UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, ScreenWidth,  315*kfit)];
        _headView.backgroundColor = [UIColor whiteColor];
        [_headView addSubview:self.yearButton];
        [_headView addSubview:self.monthButton];
//        [_headView addSubview:self.photoImage];
//        
//        [_headView addSubview:self.runningDays];
//        [_headView addSubview:self.runningDaysLabel];
//        [_headView addSubview:self.totalDays];
//        
//        [_headView addSubview:self.totalDaysLabel];
//        [_headView addSubview:self.allAccount];
//        [_headView addSubview:self.allAccountLabel];
    }
    return _headView;
}
-(UIButton *)yearButton{
    if (!_yearButton) {
        _yearButton = [[UIButton alloc]initWithFrame:CGRectMake(54*kfit, 11*kfit, 34*kfit, 28*kfit)];
        [_yearButton setTitle:@"年" forState:0];
        UIBezierPath *maskPath;
        maskPath = [UIBezierPath bezierPathWithRoundedRect:_yearButton.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight  cornerRadii:CGSizeMake(3, 3)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _yearButton.bounds;
        maskLayer.path = maskPath.CGPath;
        _yearButton.layer.mask = maskLayer;
        _yearButton.layer.borderWidth = 1;
        _yearButton.layer.borderColor = [[UIColor colorWithString:@"#A2A2A2"] CGColor];

        [_yearButton setTitleColor:[UIColor colorWithHexString:@"#A2A2A2"] forState:0];
        _yearButton.titleLabel.font = [UIFont systemFontOfSize:12*kfit];
        
    }
    return _yearButton;
}
-(UIButton *)monthButton{
    if (!_monthButton) {
        _monthButton = [[UIButton alloc]initWithFrame:CGRectMake(20*kfit, 11*kfit, 35*kfit, 28*kfit)];

        UIBezierPath *maskPath;
        maskPath = [UIBezierPath bezierPathWithRoundedRect:_monthButton.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft  cornerRadii:CGSizeMake(3, 3)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _monthButton.bounds;
        maskLayer.path = maskPath.CGPath;
        _monthButton.layer.mask = maskLayer;
//        _monthButton.titleLabel.font =[UIFont systemFontOfSize:16*kfit];
        _monthButton.titleLabel.font = [UIFont systemFontOfSize:12*kfit];
        [_monthButton setTitle:@"月" forState:0];
        [_monthButton setTitleColor:[UIColor colorWithString:@"#000000"] forState:0];
        _monthButton.backgroundColor = [UIColor colorWithHexString:@"#A2A2A2"];
    }
    return _monthButton;
}
-(UIButton *)dateButton{
    if (!_dateButton) {
        _dateButton = [[UIButton alloc]initWithFrame:CGRectMake(278*kfit, 11*kfit, 35*kfit, 28*kfit)];
        
      
        _dateButton.titleLabel.font = [UIFont systemFontOfSize:12*kfit];
        [_dateButton setTitle:@"月" forState:0];
        [_dateButton setTitleColor:[UIColor colorWithString:@"#A2A2A2"] forState:0];
    }
    return _dateButton;
}
/**创建“收益走势”图*/
-(void)createIncomeChartLineView{
    
    //    NSArray *tempDataArrOfY = @[@[@"400",@"600",@"500",@"800",@"600",@"700",@"500"]];
    //    NSArray *tempDataArrOfY1 = @[@[@"30",@"50",@"30",@"90",@"40",@"50",@"40"]];
    //
    //    _incomeChartLineView = [[LRSChartView alloc]initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 300)];
    //    //设置X轴坐标字体大小
    //    _incomeChartLineView.x_Font = [UIFont systemFontOfSize:10];
    //    //设置X轴坐标字体颜色
    //    _incomeChartLineView.x_Color = [UIColor colorWithHexString:@"0x999999"];
    //    //设置Y轴坐标字体大小
    //    _incomeChartLineView.y_Font = [UIFont systemFontOfSize:10];
    //    //设置Y轴坐标字体颜色
    //    _incomeChartLineView.y_Color = [UIColor colorWithHexString:@"0x999999"];
    //    //设置X轴数据间隔
    //    _incomeChartLineView.Xmargin = 50;
    //
    //    _incomeChartLineView.backgroundColor = [UIColor clearColor];
    //    _incomeChartLineView.chartViewStyle = LRSChartViewLeftRightLine;
    //    _incomeChartLineView.x_Font = [UIFont systemFontOfSize:10];
    //    _incomeChartLineView.x_Color = [UIColor colorWithHexString:@"0x999999"];
    //    _incomeChartLineView.isFloating = YES;
    //    //折线图数据
    //    _incomeChartLineView.leftDataArr = tempDataArrOfY;
    //    _incomeChartLineView.rightDataArr = tempDataArrOfY1;
    //    //设置图层效果
    //    _incomeChartLineView.chartLayerStyle = LRSChartProjection;
    //
    //    _incomeChartLineView.leftColorStrArr = @[@"#febf83"];
    //    _incomeChartLineView.rightColorStrArr = @[@"#53d2f8"];
    //
    //    //底部日期
    //    _incomeChartLineView.dataArrOfX = @[@"01-13",@"01-14",@"01-15",@"01-16",@"01-17",@"01-18",@"01-19"];//拿到X轴坐标
    //
    //    [_incomeChartLineView show];
    //    [self.view addSubview:_incomeChartLineView];
    
    NSArray *tempDataArrOfY = @[@[@"400",@"600",@"500",@"800",@"600",@"700",@"500",@"500",@"500",@"500"]];
    
    _incomeChartLineView = [[LRSChartView alloc]initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, 265)];
    
    
    //是否可以浮动
    _incomeChartLineView.isFloating = YES;
    //设置X轴坐标字体大小
    _incomeChartLineView.x_Font = [UIFont systemFontOfSize:10];
    //设置X轴坐标字体颜色
    _incomeChartLineView.x_Color = [UIColor colorWithHexString:@"0x999999"];
    //设置Y轴坐标字体大小
    _incomeChartLineView.y_Font = [UIFont systemFontOfSize:10];
    //设置Y轴坐标字体颜色
    _incomeChartLineView.y_Color = [UIColor colorWithHexString:@"0x999999"];
    //设置X轴数据间隔
    _incomeChartLineView.Xmargin = 50;
    //设置背景颜色
    _incomeChartLineView.backgroundColor = [UIColor clearColor];
    //是否根据折线数据浮动泡泡
    //_incomeChartLineView.isFloating = YES;
    //折线图数据
    _incomeChartLineView.leftDataArr = tempDataArrOfY;
    //折线图所有颜色
    _incomeChartLineView.leftColorStrArr = @[@"#febf83",@"#53d2f8",@"#7211df"];
    //设置折线样式
    _incomeChartLineView.chartViewStyle = LRSChartViewMoreClickLine;
    //设置图层效果
    _incomeChartLineView.chartLayerStyle = LRSChartProjection;
    //设置折现效果
    _incomeChartLineView.lineLayerStyle = LRSLineLayerNone;
    //渐变效果的颜色组
    _incomeChartLineView.colors = @[@[[UIColor colorWithHexString:@"#febf83"],[UIColor greenColor]],@[[UIColor colorWithHexString:@"#53d2f8"],[UIColor blueColor]],@[[UIColor colorWithHexString:@"#7211df"],[UIColor redColor]]];
    //渐变开始比例
    _incomeChartLineView.proportion = 0.5;
    //底部日期
    _incomeChartLineView.dataArrOfX = @[@"01-13",@"01-14",@"01-15",@"01-16",@"01-17",@"01-18",@"01-19",@"01-20",@"01-21",@"01-22"];
    //开始画图
    [_incomeChartLineView show];
    [self.headView addSubview:_incomeChartLineView];
    
    
    
    
}
-(void)rightPress:(UIButton *)sender{
    
    if (sender.tag ==1000) {
        [self.incomeChartLineView removeFromSuperview];
        [self.headView addSubview:pieChartView];
        [pieChartView animationadd];
        sender.tag = 1001;
    }else{
        
        sender.tag = 1000;
        [pieChartView removeFromSuperview];
        [self.headView addSubview:self.incomeChartLineView];
    }
    
}
- (void)addpie{
    
    PieModel *mo = [[PieModel alloc]init];
    mo.count = 150;
    mo.title = @"面包";
    mo.descript = @"全麦面包";
    mo.color = PNBlue;
    
    PieModel *mo1 = [[PieModel alloc]init];
    mo1.count = 20;
    mo1.title = @"威化饼";
    mo1.descript = @"巧克力威化";
    mo1.color = PNDeepGreen;
    
    PieModel *mo2 = [[PieModel alloc]init];
    mo2.count = 140;
    mo2.title = @"饼干";
    mo2.descript = @"葱油味饼干";
    mo2.color = PNYellow;
    
    PieModel *mo3 = [[PieModel alloc]init];
    mo3.count = 90;
    mo3.title = @"巧克力";
    mo3.descript = @"榛子味巧克力";
    mo3.color = PNBrown;
    
    PieModel *mo4 = [[PieModel alloc]init];
    mo4.count = 120;
    mo4.title = @"牛奶";
    mo4.descript = @"脱脂牛奶";
    mo4.color = PNMauve;
    
    pieChartView = [[WLPieView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 280) withModels:@[mo,mo1,mo2,mo3,mo4]];
//    pieChartView.center = self.headView.center;
//    pieChartView.backgroundColor = [UIColor colorWithRed:244/255.f green:244/255.f blue:244/255.f alpha:1];
//    [self.view addSubview:pieChartView];
    
}

-(UITableView *)Tab{
    
    if (!_Tab) {
        _Tab = [[UITableView alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight, ScreenWidth, ScreenHeight-SafeAreaTopHeight-SafeAreaTabbarHeight)];
        _Tab.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        _Tab.delegate = self;
        _Tab.dataSource = self;
        //        self.myTab.scrollEnabled = NO;
        _Tab.showsVerticalScrollIndicator = NO;
        _Tab.tableHeaderView = self.headView;
        _Tab.tableFooterView.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        if(@available(iOS 11.0, *)){
            _Tab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _Tab.estimatedRowHeight = 0;
            _Tab.estimatedSectionHeaderHeight = 0;
            _Tab.estimatedSectionFooterHeight = 0;
            
        }
        
    }
    
    return _Tab;
}
#pragma mark -设置cell横线左顶边
-(void)viewDidLayoutSubviews {
    if ([self.Tab respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.Tab setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.Tab respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.Tab setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
#pragma  make - UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
   
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    chartTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (!cell) {
        
        cell = [[chartTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        
    }
//    cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.nameLabel.text = @"餐饮";
    return cell;
}

#pragma  make - UITableViewDelegate
//返回高度，计算不同高度cell 一定要用，否则会出现偏移
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return 70;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    view.backgroundColor = [UIColor colorWithHexString:@"#F6F6F8"];
    //    UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(21*kfit, 0, ScreenWidth/2, 35)];
    //    leftLabel.textColor = [UIColor colorWithHexString:@"#B7B7B7"];
    //    leftLabel.font = [UIFont systemFontOfSize:10*kfit];
    //    leftLabel.textAlignment = NSTextAlignmentLeft;
    //    leftLabel.text =self.sectionArray[section][@"data"];
    //
    //    UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2 -21*kfit, 0, ScreenWidth/2, 35)];
    //    rightLabel.textColor = [UIColor colorWithHexString:@"#B7B7B7"];
    //    rightLabel.font = [UIFont systemFontOfSize:10*kfit];
    //    rightLabel.textAlignment = NSTextAlignmentRight;
    //    rightLabel.text =self.sectionArray[section][@"money"];
    //
    //    [view addSubview:leftLabel];
    //    [view addSubview:rightLabel];
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
   
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    [super viewWillDisappear:animated];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
